package logic

import (
	"book-hub/models"
	"context"

	"book-hub/bookrpc/bookrpc"
	"book-hub/bookrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *bookrpc.CollectListRequest) (*bookrpc.CollectListResponse, error) {
	// todo: add your logic here and delete this line
	var collect []models.CollectData
	models.DB.Model(models.Collect{}).
		Select(" novels.*").
		Joins("LEFT JOIN novels ON collects.novel_id = novels.id").
		Where("collects.user_id=?", in.UserId).
		Debug().
		Find(&collect)
	var listData []*bookrpc.CollectList
	for _, data := range collect {
		list := bookrpc.CollectList{
			Title:        data.Title,
			Class:        data.Class,
			Label:        data.Label,
			Introduction: data.Introduction,
			UserId:       int64(data.Novel.UserId),
			CollectCount: int64(data.CollectCount),
		}
		listData = append(listData, &list)
	}
	return &bookrpc.CollectListResponse{
		List: listData,
	}, nil
}
